﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using vensoft.BussinesLogic;
using vensoft.Entities;

namespace vensoft.Win32
{
    public partial class frmGestionarCliente : Form
    {
        IClienteBL clienteBL;
        //List<Cliente> listaTemporal;

        public frmGestionarCliente()
        {
            InitializeComponent();
            clienteBL = new ClienteBL();
        }

        public Int32 estadoBotones { get; set; }

        private void frmGestionarCliente_Load(object sender, EventArgs e)
        {
            // TODO: esta línea de código carga datos en la tabla 'vensoftDataSet1.GetAllFromCliente' Puede moverla o quitarla según sea necesario.
            
            clienteBL = new ClienteBL();
            clienteBindingSource.DataSource = clienteBL.GetAllFromCliente();
            if (estadoBotones == 0)
            {
                btnSeleccionar.Visible = true;
                btnNuevo.Visible = false;
                btnEditar.Visible = false;
                btnEliminar.Visible = false;
            }
            else
            {
                btnSeleccionar.Visible = false;
                btnNuevo.Visible = true;
                btnEditar.Visible = true;
                btnEliminar.Visible = true;
            }            
           
        }

        private void txtBuscar_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                clienteBL = new ClienteBL();
                clienteBindingSource.DataSource = clienteBL.GetFromClienteByDescripcion(txtBuscarCliente.Text);
            }
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            frmAECliente aecliente = new frmAECliente();
            aecliente.CurrentClient = clienteBindingSource.Current as Cliente;
            aecliente.Editar();
            aecliente.ShowDialog();

            //clienteBindingSource.ResetBindings(true); 
        }

        public Cliente cliente { get; set; }

        private void btnSeleccionar_Click(object sender, EventArgs e)
        {
            //frmRegistrarVenta venta = new frmRegistrarVenta();
            //venta.CurrentClient = clienteBindingSource.Current as Cliente;
            //venta.LlenarCliente(); 
            //venta.ShowDialog();   

            Cliente clienteSelect = clienteBindingSource.Current as Cliente;
            if (clienteSelect != null)
            {
                cliente = clienteSelect;
            }
            Close();

            //CurrentClient = clienteBindingSource.Current as Cliente; 
            //this.Close();
        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            frmAECliente aecliente = new frmAECliente();
            aecliente.Nuevo();
            aecliente.ShowDialog();
        }

        public Int32 Validacion { get; set; }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            Validacion = 1;
            this.Close();
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("¿Estas seguro que deseas eliminar?", "ALERTA", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Cliente curentClient = clienteBindingSource.Current as Cliente;
                clienteBL.EliminarCliente(curentClient);
                clienteBindingSource.RemoveCurrent();
            }
        }

        private void btnActualizarDatos_Click(object sender, EventArgs e)
        {
            clienteBL = new ClienteBL();
            clienteBindingSource.DataSource = clienteBL.GetAllFromCliente();
            //clienteBindingSource.ResetBindings(true);
            //txtBuscarCliente.Text = null;
        }

        public void txtBuscar_Click(Object sender, MouseEventArgs e)
        {
            switch (e.Button)
            {
                case (MouseButtons.Left):
                    txtBuscarCliente.Text = null;                    
                    break;
            }
        }

        public Cliente CurrentClient { get; set; }



    }
}
